package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mapper.ImgsMapper;
import com.woniu.pojo.Imgs;
import com.woniu.service.IImgsService;
import com.woniu.vo.ImgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanMax
 * @since 2022-10-19
 */
@Service
public class ImgsServiceImpl extends ServiceImpl<ImgsMapper, Imgs> implements IImgsService {
    @Autowired
    private ImgsMapper imgsMapper;

    @Override
    public Integer addImgs(ImgVo imgVo) {
        if (imgVo!=null){
            int insert = imgsMapper.insert(imgVo.getImgs());
            if (imgVo.getImageList()!=null){
                for (String imgss : imgVo.getImageList()) {
                    if (!imgss.equals(imgVo.getImgs().getImg())){
                        Imgs imgs1 = new Imgs();
                        imgs1.setGoodsId(imgVo.getImgs().getGoodsId());
                        imgs1.setImg(imgss);
                        imgsMapper.insert(imgs1);
                    }
                }
            }
            return insert;
        }

        return 0;
    }

    @Override
    public Integer editImgs(ImgVo imgVo) {
        QueryWrapper<Imgs> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",imgVo.getImgs().getGoodsId());
        imgsMapper.delete(wrapper);
        Integer row=0;
        if (imgVo!=null){
                row = imgsMapper.insert(imgVo.getImgs());
            if (imgVo.getImageList()!=null){
                for (String imgss : imgVo.getImageList()) {
                    if (!imgss.equals(imgVo.getImgs().getImg())){
                        Imgs imgs1 = new Imgs();
                        imgs1.setGoodsId(imgVo.getImgs().getGoodsId());
                        imgs1.setImg(imgss);
                        imgsMapper.insert(imgs1);
                    }
                }
            }
            return row;
        }

        return 0;
    }
}
